<template>
  <div class="score-card-view">
    <el-empty v-if="!loading && (!data || data.length === 0)" description="暂无成绩数据" />

    <el-skeleton v-else-if="loading" :rows="3" animated />

    <div v-else class="card-grid">
      <el-card
        v-for="item in data"
        :key="item.id"
        class="score-card"
        shadow="hover"
        @click="handleCardClick(item)"
      >
        <template #header>
          <div class="card-header">
            <div class="project-info">
              <span class="project-name">{{ item.project?.name || '-' }}</span>
              <el-tag size="small" :type="getStatusType(item.status)">
                {{ getStatusText(item.status) }}
              </el-tag>
            </div>
            <div class="ranking" v-if="item.rank">
              <span class="rank-badge" :class="getRankingClass(item.rank)">
                <span v-if="item.rank <= 3" class="medal">{{ getMedalIcon(item.rank) }}</span>
                <span v-else class="rank-number">第{{ item.rank }}名</span>
              </span>
            </div>
          </div>
        </template>

        <div class="card-content">
          <div class="student-section">
            <div class="student-avatar">
              <el-avatar :size="50" :src="item.student?.avatar">
                {{ item.student?.name?.charAt(0) || '学' }}
              </el-avatar>
            </div>
            <div class="student-info">
              <div class="student-name">{{ item.student?.name || '-' }}</div>
              <div class="student-details">
                <div class="student-id">{{ item.student?.studentNumber || '-' }}</div>
                <div class="student-department">{{ item.student?.department || '-' }}</div>
              </div>
            </div>
          </div>

          <div class="score-section">
            <div class="score-display">
              <span class="score-value" :class="{ 'record-breaking': item.isRecord }">
                {{ item.score || '-' }}
              </span>
              <span class="score-unit">{{ item.unit || '' }}</span>
              <el-tag v-if="item.isRecord" type="danger" size="small" class="record-tag">
                <el-icon><Trophy /></el-icon> 破纪录
              </el-tag>
            </div>
            <div class="score-meta">
              <span class="round-info">{{ getRoundText(item.round) }}</span>
              <span class="time-info">{{ formatDateTime(item.createTime) }}</span>
            </div>
          </div>

          <div class="action-section">
            <el-button-group size="small">
              <el-button type="primary" @click.stop="handleViewDetail(item)">
                <el-icon><View /></el-icon> 详情
              </el-button>
              <el-button type="success" @click.stop="handleGenerateCertificate(item)">
                <el-icon><Document /></el-icon> 证书
              </el-button>
              <el-button type="info" @click.stop="handleShare(item)">
                <el-icon><Share /></el-icon> 分享
              </el-button>
            </el-button-group>

            <!-- 审核相关操作 -->
            <el-button-group v-if="item.status === 'PENDING'" size="small" class="audit-actions">
              <el-button type="success" @click.stop="handleApprove(item)">
                <el-icon><Select /></el-icon> 通过
              </el-button>
              <el-button type="danger" @click.stop="handleReject(item)">
                <el-icon><Close /></el-icon> 驳回
              </el-button>
            </el-button-group>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 分页组件 -->
    <div v-if="data && data.length > 0" class="pagination-section">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[12, 24, 48]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import {
  Trophy, View, Document, Share, Select, Close
} from '@element-plus/icons-vue'

// 组件属性
const props = defineProps({
  data: {
    type: Array,
    default: () => []
  },
  loading: {
    type: Boolean,
    default: false
  }
})

// 组件事件
const emit = defineEmits([
  'view-detail',
  'generate-certificate',
  'share',
  'approve',
  'reject'
])

// 分页数据
const currentPage = ref(1)
const pageSize = ref(12)
const total = computed(() => props.data?.length || 0)

// 获取排名样式类
const getRankingClass = (rank) => {
  if (rank === 1) return 'gold'
  if (rank === 2) return 'silver'
  if (rank === 3) return 'bronze'
  return ''
}

// 获取奖牌图标
const getMedalIcon = (rank) => {
  if (rank === 1) return '🥇'
  if (rank === 2) return '🥈'
  if (rank === 3) return '🥉'
  return ''
}

// 获取轮次文本
const getRoundText = (round) => {
  const roundMap = {
    1: '第1轮',
    2: '第2轮',
    3: '第3轮',
    4: '决赛'
  }
  return roundMap[round] || '-'
}

// 获取状态类型
const getStatusType = (status) => {
  const typeMap = {
    'PENDING': 'warning',
    'PUBLISHED': 'success',
    'REJECTED': 'danger',
    'DISPUTED': 'info'
  }
  return typeMap[status] || ''
}

// 获取状态文本
const getStatusText = (status) => {
  const textMap = {
    'PENDING': '待审核',
    'PUBLISHED': '已公示',
    'REJECTED': '已驳回',
    'DISPUTED': '有异议'
  }
  return textMap[status] || '-'
}

// 格式化日期时间
const formatDateTime = (dateTime) => {
  if (!dateTime) return '-'
  const date = new Date(dateTime)
  return date.toLocaleString('zh-CN', {
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 处理卡片点击
const handleCardClick = (item) => {
  handleViewDetail(item)
}

// 处理查看详情
const handleViewDetail = (item) => {
  emit('view-detail', item)
}

// 处理生成证书
const handleGenerateCertificate = (item) => {
  emit('generate-certificate', item)
}

// 处理分享
const handleShare = (item) => {
  emit('share', item)
}

// 处理审核通过
const handleApprove = (item) => {
  emit('approve', item)
}

// 处理驳回
const handleReject = (item) => {
  emit('reject', item)
}

// 处理分页大小变化
const handleSizeChange = (size) => {
  pageSize.value = size
  currentPage.value = 1
}

// 处理当前页变化
const handleCurrentChange = (page) => {
  currentPage.value = page
}
</script>

<style scoped>
.score-card-view {
  width: 100%;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.score-card {
  transition: all 0.3s ease;
  border-radius: 12px;
  cursor: pointer;
  border: none;
}

.score-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.project-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.project-name {
  font-weight: 600;
  font-size: 16px;
  color: #2c3e50;
}

.ranking {
  display: flex;
  align-items: center;
}

.rank-badge {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  height: 40px;
  border-radius: 50%;
  font-weight: bold;
}

.rank-badge.gold {
  background: linear-gradient(135deg, #ffd700, #ffed4e);
  color: #b8860b;
  box-shadow: 0 2px 8px rgba(255, 215, 0, 0.3);
}

.rank-badge.silver {
  background: linear-gradient(135deg, #c0c0c0, #e8e8e8);
  color: #696969;
  box-shadow: 0 2px 8px rgba(192, 192, 192, 0.3);
}

.rank-badge.bronze {
  background: linear-gradient(135deg, #cd7f32, #e4a853);
  color: #8b4513;
  box-shadow: 0 2px 8px rgba(205, 127, 50, 0.3);
}

.medal {
  font-size: 20px;
}

.rank-number {
  font-size: 12px;
  padding: 0 8px;
}

.card-content {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.student-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.student-info {
  flex: 1;
}

.student-name {
  font-weight: 500;
  font-size: 16px;
  color: #2c3e50;
  margin-bottom: 4px;
}

.student-details {
  font-size: 12px;
  color: #7f8c8d;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.score-section {
  background-color: #f8f9fa;
  padding: 12px;
  border-radius: 8px;
}

.score-display {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.score-value {
  font-size: 24px;
  font-weight: bold;
  color: #2c3e50;
}

.score-value.record-breaking {
  color: #e74c3c;
  animation: pulse 1.5s infinite;
}

.score-unit {
  font-size: 14px;
  color: #7f8c8d;
}

.record-tag {
  display: flex;
  align-items: center;
  gap: 4px;
}

.score-meta {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #7f8c8d;
}

.action-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
}

.audit-actions {
  margin-top: 8px;
}

.pagination-section {
  display: flex;
  justify-content: center;
  padding: 20px 0;
}

/* 动画效果 */
@keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.8;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .card-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }

  .project-name {
    font-size: 14px;
  }

  .student-name {
    font-size: 14px;
  }

  .score-value {
    font-size: 20px;
  }

  .action-section {
    flex-direction: column;
    align-items: stretch;
  }
}
</style>